package cn.chiship.framework.business.biz.global;


import cn.chiship.framework.business.biz.business.service.TestExampleService;
import cn.chiship.framework.business.core.common.BusinessCommonConstants;
import cn.chiship.framework.common.annotation.LoginUser;
import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.sdk.cache.vo.CacheUserVO;
import cn.chiship.sdk.core.annotation.NoParamsSign;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.enums.BaseResultEnum;
import cn.chiship.sdk.core.id.SnowflakeIdUtil;
import cn.chiship.sdk.core.util.DateUtils;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.framework.util.ServletUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Map;

/**
 * controller
 * 2020/8/5
 *
 * @author lijian
 */
@RestController
@RequestMapping("/import")
@Api(tags = "数据导入控制器")
public class ImportController {

    private static final String STRING_CLASS_NAME = "className";
    @Resource
    private TestExampleService testExampleService;


    @SystemOptionAnnotation(describe = "根据类导入数据")
    @ApiOperation(value = "根据类导入数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "className", value = "类型", required = true,dataTypeClass =String.class, paramType = "path")
    })
    @PostMapping(value = "/data")
    @NoParamsSign
    public ResponseEntity<BaseResult> importData(@RequestParam("file") MultipartFile multipartFile, @ApiIgnore @LoginUser CacheUserVO cacheUserVO) throws Exception {
        if (multipartFile.isEmpty()) {
            return new ResponseEntity<>(BaseResult.error("上传失败，请选择文件"), HttpStatus.OK);
        }
        Map<String, Object> paramMap = ServletUtil.getParameterValues();
        paramMap.put("fileName", multipartFile.getOriginalFilename());
        paramMap.put("contentType", multipartFile.getContentType());
        paramMap.put("userVo",cacheUserVO);
        String className = paramMap.get(STRING_CLASS_NAME).toString();
        InputStream is = multipartFile.getInputStream();
        String taskId = DateUtils.dateTimeNow() + SnowflakeIdUtil.generateId();
        switch (className) {
            /**
             * 测试示例
             */
            case BusinessCommonConstants.EXPORT_IMPORT_CLASS_TEST_EXAMPLE:
                testExampleService.asyncImportData(is, taskId, paramMap);
                break;
            default:
                return new ResponseEntity<>(BaseResult.error(BaseResultEnum.FAILED, "不支持的数据导入此表:" + className), HttpStatus.OK);
        }
        return new ResponseEntity<>(BaseResult.ok(taskId), HttpStatus.OK);

    }

    @ApiOperation(value = "根据任务ID及类获取异步任务实时进度")
    @PostMapping("/getProcessStatus")
    @NoParamsSign
    public ResponseEntity getProcessStatus(@RequestBody Map<String, Object> paramsMap) {
        String taskId = paramsMap.get("taskId").toString();
        String className = paramsMap.get(STRING_CLASS_NAME).toString();
        if (StringUtil.isNullOrEmpty(taskId)) {
            return new ResponseEntity<>(BaseResult.error("缺少参数[taskId]"), HttpStatus.OK);

        }
        if (StringUtil.isNullOrEmpty(className)) {
            return new ResponseEntity<>(BaseResult.error("缺少参数[className]"), HttpStatus.OK);
        }
        BaseResult baseResult;
        switch (className) {
            /**
             * 测试示例
             */
            case BusinessCommonConstants.EXPORT_IMPORT_CLASS_TEST_EXAMPLE:
                baseResult = testExampleService.getImportProcessStatus(taskId);
                break;
            default:
                baseResult = BaseResult.error("不支持的查看此表[" + className + "]进度");
        }
        return new ResponseEntity<>(baseResult, HttpStatus.OK);
    }
}
